package dolf.objects.weapons.Electricity;


import java.awt.Graphics2D;
import java.util.LinkedList;
import java.util.List;
import java.awt.Color;
import javax.swing.ImageIcon;
import dolf.game.*;
import dolf.objects.ImageStore;
import dolf.objects.WorldObject;
import dolf.objects.weapons.*;
import dolf.objects.*;
import dolf.window.SystemMessagesHandler;

public class Electricity extends CircleObject implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 115;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Electricity.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Electricity.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Electricity.lang.lang").getString("description");
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/Electricity/gfx/icon.png");

	private int shocks;
	private final int MAX_SHOCKS = 3;
	private final int DAMAGE = 10;
	private final int DAM_RAD = 40;
	private int TIMER_RATE = 10;
	private int timer;
	private Player pl;
	private boolean inititalized = false;
	private boolean aktivatable = true;
	private List<Ball> victims = new LinkedList<Ball>(); // a list of all victims hit by a shock
	
	public Electricity(){
		super(null,1,false,true);
		this.setCollitabe(false);
	}
	
		
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.shocks = this.MAX_SHOCKS;
		this.timer = 0;
		aktivatable = true;
		this.inititalized = true;
		this.pl = _pl;
		this.pl.setWeaponState();
	}
	public void deInitialize(){
		this.inititalized = false;
		this.pl = null;
	}
	
	public void execute() {
		if(!this.aktivatable && !Level.getInstance().mousePressed()){
			this.aktivatable = true;
		}
		
		if(Level.getInstance().mousePressed()&&pl.getBall().isMoving()&& this.aktivatable){
			if(this.shocks > 0){
				this.shocks--;
				this.timer = this.TIMER_RATE;
				this.aktivatable = false;
				this.pl.setWeaponState();
				this.victims = new LinkedList<Ball>();
				for(int i = 0; i < Level.getInstance().getPlayers().size();i++){
					Ball b = Level.getInstance().getPlayers().get(i).getBall();
					if(	b.getPlayer() != this.pl && b.isVisible()
							&& this.pl.getBall().getPosition().getDist(b.getPosition()) <= this.DAM_RAD){
						this.victims.add(b);//b.getPosition().getCopy());
						b.getPlayer().removeHP(this.DAMAGE);
						this.pl.addDamage(this.DAMAGE);
						System.out.println("TZZZZZ");
					}
				}
				//if(this.victims.size()>0){
				Level.getInstance().registerObjectOnTop(this);
				//}
			} else {
				System.err.println("out of Battery!");
			}
		}
	}
	
	public void paint(Graphics2D g){
		if(timer > 0){
			//if(timer == this.TIMER_RATE){
				Color col = g.getColor();
				g.setColor(Color.blue.brighter());
				if(this.victims.isEmpty()){
					int rnd = (int)(Math.random()*5)+5;
					for(int i = 0; i < rnd; i++){
						double dire = (Math.floor((Math.random() * 360))-180)*Math.PI/180;
						double leng = Math.floor((Math.random() * pl.getBall().getRadius()));
						Point p = new Point(Math.cos(dire)*leng+pl.getBall().getPosition().getX(),Math.sin(dire)*leng+pl.getBall().getPosition().getY());
					//	System.out.println(p.print());
						g.drawLine(pl.getBall().getPosition().getIntX(), pl.getBall().getPosition().getIntY(), p.getIntX(), p.getIntY());
					}
				}
				for(int i = 0; i<this.victims.size(); i++){
					List<Point> tmp = new LinkedList<Point>();
					tmp = this.createLightning(this.victims.get(i));
					for(int j = 0; j<tmp.size()-1;j++ ){
						g.drawLine(tmp.get(j).getIntX(), tmp.get(j).getIntY(), tmp.get(j+1).getIntX(), tmp.get(j+1).getIntY());
					}
				}
				g.setColor(col);

			//}
			timer--;
			//g.drawImage(this.image, null, this.getPosition().getIntX()-this.DAM_RAD, this.getPosition().getIntY()-this.DAM_RAD);
		} else {
			Level.getInstance().removeObject(this);
		}
	}
	
	public boolean newMotion(WorldObject object){
		return true;
	}	

	public ImageIcon getIcon() {
		return Icon; //ImageStore.getInstance().getIcon("dolf/objects/weapons/Electricity/gfx/icon.png");	
	}

	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}

	public int getPrice() {
		return Price;
	}
	public String getType(){
		return WeaponType;
	}
	
	public Point getPosition(){
		return this.pl.getBall().getPosition().getCopy();
	}

	public Player getPlayer(){
		return this.pl;
	}
	
	public void remove(){
		pl.removeWeapon(this);
		this.pl.setWeaponState();
		this.pl.setAktWeapon(null);
	}
	public double getState(){
		return (double)this.shocks/this.MAX_SHOCKS;
	}
	
	private List<Point> createLightning(Ball b){
		Point p = b.getPosition();
		List<Point> erg = new LinkedList<Point>();
		Point tmp = pl.getBall().getPosition().getCopy();
		erg.add(tmp.getCopy());
		double dist = p.getDist(tmp);
		Vector dire = p.sub(tmp).getCopy();
		Vector senk = dire.getNormVec();
		int z = ((int) dist) / 3;
		for(int i = z;i>0; i-- ){
			Vector vec = dire.setLength(3);
			tmp = tmp.add(vec);
			int rnd = (int)(Math.random()*5) *(i%2==1?-1:1);
			erg.add(tmp.add(senk.setLength(rnd)));
		//	System.out.println(erg.get(erg.size()-1));
		}
		erg.add(p.getCopy());
		return erg;
	}
}
